home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 January: Mac OS SDK / Dev.CD Jan 99 SDK1.toast / Development Kits / Mac OS USB DDK_v1.0.1 / Examples / UniversalModule / UniversalConfigParse.c next >
Encoding:
C/C++ Source or Header  |  1998-09-03  |  4.0 KB  |  112 lines  |  [TEXT/MPS ]

  1. /*
  2.     File:        UniversalConfigParse.c
  3.  
  4.     Contains:    xxx put contents here xxx
  5.  
  6.     Version:    xxx put version here xxx
  7.  
  8.     Copyright:    © 1998 by Apple Computer, Inc., all rights reserved.
  9.  
  10. */
  11.  
  12. #include <Types.h>
  13. #include <Devices.h>
  14. #include <processes.h>
  15. #include <DriverServices.h>
  16.  
  17. #include <USB.h>
  18.  
  19. #include "UniversalModule.h"
  20.  
  21. // we need to find the interface descriptor and everything it owns
  22. // since the interface descriptor we were passed may not have had 
  23. // the other descriptors following it. We go to the config
  24. // descriptor, and look forward until we find the interface with
  25. // the number we are looking for. we know that starting from that
  26. // point are all our endpoint descriptors
  27. OSErr FindHIDInterfaceByInterfaceNumber(LogicalAddress pConfigDesc, UInt32 interfaceNumber, USBInterfaceDescriptorPtr * hInterfaceDesc)
  28. {
  29. UInt32 totalLength;
  30. void * pEndOfDescriptors;
  31. USBInterfaceDescriptorPtr     pMyIntDesc;
  32. USBDescriptorHeaderPtr        pCurrentDesc;
  33. unsigned long                anAddress, anOffset;
  34.  
  35.     totalLength = ((USBConfigurationDescriptorPtr)pConfigDesc)->totalLength;
  36.     pEndOfDescriptors = (Ptr)pConfigDesc + totalLength;                    // get the total length and add it to the start of the config space
  37.     pCurrentDesc = (USBDescriptorHeaderPtr)pConfigDesc;                    // point the currentdesc to the start of the config space
  38.     
  39.     while (pCurrentDesc < pEndOfDescriptors)                            // as long as we haven't exhausted all the descriptors
  40.     {
  41.         if (pCurrentDesc->descriptorType == kUSBInterfaceDesc)            // look at the current descriptor
  42.         {
  43.             pMyIntDesc = (USBInterfaceDescriptorPtr)pCurrentDesc;        // if it's an interface descriptor
  44.             if ((pMyIntDesc->interfaceNumber == interfaceNumber))        // see if it's the interface number we want...
  45.             {
  46.                 *hInterfaceDesc = pMyIntDesc;                            // if it is, then return with hInterfaceDesc set to the
  47.                 return noErr;                                            // current descriptor pointer
  48.             }
  49.         }
  50.  
  51.         anAddress = (unsigned long) pCurrentDesc;                        // Nope, that either wasn't an interface descriptor
  52.         anOffset  = (unsigned long) pCurrentDesc->length;
  53.         anAddress += anOffset;
  54.         pCurrentDesc = (USBDescriptorHeaderPtr) anAddress;
  55.  
  56.         if (pCurrentDesc->length == 0)
  57.             break;
  58.     }                                                                    // or it was, but not the droid we're looking for.
  59.     *hInterfaceDesc = NULL;
  60.     return kUSBInternalErr;
  61. }
  62.  
  63.  
  64. // this returns a pointer to the first HID descriptor in the configuration
  65. // _and_ all the HID report and HID Physical descripters follow it
  66. // the caller has to be smart enough to parse it correctly, and
  67. // to know not to read too far (as any other configurations with their
  68. // corresponding HID descriptors) might follow
  69. OSErr GetHIDDescriptors(USBConfigurationDescriptorPtr pConfigDesc, USBHIDDescriptorPtr * hHIDDesc)
  70. {
  71. UInt32 totalLength;
  72. void * pEndOfDescriptors;
  73. USBDescriptorHeaderPtr    pCurrentDesc;
  74. unsigned long    anAddress, anOffset;
  75.  
  76.     // clear return value, in case we fail (should we do this, or leave it unchanged?)
  77.     *hHIDDesc = nil;
  78.     
  79.     totalLength = pConfigDesc->totalLength;
  80.     
  81.     pEndOfDescriptors = (Ptr)pConfigDesc + totalLength;
  82.     pCurrentDesc = (USBDescriptorHeaderPtr)pConfigDesc;
  83.     
  84.     if (pCurrentDesc)
  85.     {
  86.         if (pCurrentDesc->descriptorType == kUSBInterfaceDesc)            // make certain pInterfaceDesc points to an interface 
  87.         {                                                                // it does...
  88.  
  89.             anAddress = (unsigned long) pCurrentDesc;                    // Nope, point to the next descriptor
  90.             anOffset  = (unsigned long) pCurrentDesc->length;
  91.             anAddress += anOffset;
  92.             pCurrentDesc = (USBDescriptorHeaderPtr) anAddress;            // point to the next descriptor
  93.  
  94.             while (pCurrentDesc < pEndOfDescriptors)                    // and as long as we don't go past the end
  95.             {                                                            //
  96.                 if (pCurrentDesc->descriptorType == kUSBInterfaceDesc)    // if we find *another* interface descriptor
  97.                     return kUSBInternalErr;                                // then bail.
  98.                 else
  99.                 {
  100.                     if (pCurrentDesc->descriptorType == kUSBHIDDesc)    // if we find a HID descriptor (we'll return the 1st one) •••
  101.                     {
  102.                         *hHIDDesc = (USBHIDDescriptorPtr)pCurrentDesc;
  103.                         return noErr;                                    // then return a pointer to it.
  104.                     }
  105.                 }
  106.             }
  107.         }
  108.     }
  109.     
  110.     return kUSBInternalErr;
  111. }
  112.